package com.hyz.service.impl;

import client.LogClient;
import com.alibaba.fastjson.JSON;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.assetRetire.AssetRetireResponse;
import com.hyz.pojo.assetRetire.InsertAssetRetireRequest;
import com.hyz.service.RetireApplicantService;
import com.hyz.util.Conversion;
import com.hyz.util.GetValue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.log.assetRetire.AssetRetire;
import pojo.log.assetRetire.AssetRetireResultEnum;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RetireApplicantServiceImpl implements RetireApplicantService {
    @Autowired
    private GetValue getValue;

    @Autowired
    private Conversion conversion;

    @Autowired
    private LogClient logClient;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult<List<AssetRetireResponse>> insertAssetRetire(InsertAssetRetireRequest request) {
        // TODO: 获取用户id
        Long retireApplicant = 1L;

        //接收报废的资产id和理由，初始化报废申请实体类
        AssetRetire assetRetire = new AssetRetire();
        assetRetire.setId(getValue.getUuid());
        assetRetire.setAssetId(request.getAssetId());
        assetRetire.setApplicantId(retireApplicant);
        assetRetire.setRetireReason(request.getRetireReason());
        assetRetire.setResult(AssetRetireResultEnum.CREATE);
        String assetRetireJson = JSON.toJSONString(assetRetire);
        //将报废申请发送至队列
        rabbitTemplate.convertAndSend("assetRetire",
                            "insertAssetRetire",
                                    assetRetireJson);

        //根据用户id获取资产报废申请列表
        assetRetire = new AssetRetire();
        assetRetire.setApplicantId(retireApplicant);
        List<AssetRetire> assetRetires = logClient.getAssetRetires(assetRetire);
        List<AssetRetireResponse> responses = conversion.getAssetRetireResponse(assetRetires);
        return new ResponseResult<>(200, "成功", responses);
    }
}
